package com.mochimedia.ui {
	
    import com.mochimedia.ui.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
    
    /**
    * ...
    * @author $(DefaultUser)
    */
    
    
    public class TabGroup extends Cell {
        
        private var _textLabels:Array;
        private var _tabs:Object;
        private var _tabsHeight:Number;
        
        private var _defaultTabIndex:Number;
        private var _activeTab:BButton;
        
        public function get activeTab():BButton { return _activeTab; }
    
        public function get tabs():Object { return _tabs; }
        
        
        //
        //
        public function TabGroup (container:Sprite, textLabels:Array, defaultTabIndex:Number = 0, height:Number = NaN, position:Position = null, style:Style = null) {
            
            init_TabGroup(container, textLabels, defaultTabIndex, height, position, style);
            
            if (_container != null) create();
            
        }
        
        //
        //
        private function init_TabGroup (container:Sprite, textLabels:Array, defaultTabIndex:Number = 0, height:Number = NaN, position:Position = null, style:Style = null):void {
            
            super.init_Cell(container, 0, 0, false, false, 0, position, style);
			
			_type = "tabgroup";
    
            _textLabels = textLabels;
            _defaultTabIndex = defaultTabIndex;
            _tabsHeight = height;
            _tabs = [];
    
        }
        
        //
        //
        override public function create ():void {
     
            super.create();
            
            if (_parentCell != null) {
                _width = _parentCell.width;
            }
            
            var i:Number;
            
            var tabsPos:Position = new Position(null, Position.ALIGN_LEFT, Position.PLACEMENT_FLOAT, Position.CLEAR_NONE, 0);
           
            for (i = 0; i < _textLabels.length; i++) {
                
                _tabs[i] = _tabs[_textLabels[i]] = (new BButton(null, _textLabels[i], Position.ALIGN_CENTER, NaN, _tabsHeight, true, true, false, tabsPos, _style));
                addChild(BButton(_tabs[i]));
                
            }
            
            _height = 0;
            
            for (i = 0; i < _tabs.length; i++) {
                
                BButton(_tabs[i]).addEventListener(EVENT_CLICK, activateTab);
                _height = Math.max(_height, BButton(_tabs[i]).height);
                
            }          
            
            _width = _parentCell.width - _position.margin_left - _position.margin_right;
            _height = _parentCell.height - _position.margin_top - _position.margin_bottom;
    
			collapse = true;
            Position.arrangeContent(this);
			
			_height = Position.getCellContentHeight(this);
			
            if (_defaultTabIndex >= 0 && _defaultTabIndex < _textLabels.length) {
                BButton(_tabs[_defaultTabIndex]).select();
                _activeTab = BButton(_tabs[_defaultTabIndex]);
            }
            
        }
        
        //
        //
        public function activateTab (e:Event):void {

            
            for (var i:int = 0; i < _tabs.length; i++) if (_tabs[i] != e.target) BButton(_tabs[i]).deselect();
            
            _activeTab = BButton(e.target);
    
            _activeTab.select();
            
            dispatchEvent(new Event(EVENT_CLICK));
            
        }
        
        //
        //
        public function getTabByLabel (textLabel:String):BButton {
            
            for (var i:int = 0; i < _tabs.length; i++) if (BButton(_tabs[i]).textLabel == textLabel) return BButton(_tabs[i]);
    
            return null;
            
        }
        
        //
        //
        public function select (tab:Object):void {

            
            var tabIndex:String = String(tab);
    
            for (var tabRef:String in _tabs) if (tabRef != tabIndex) BButton(_tabs[tabRef]).deselect();
            
            _activeTab = BButton(_tabs[tabIndex]);
            
            _activeTab.select();
            
            dispatchEvent(new Event(EVENT_CLICK));     
            
        }
        
    }
}
